﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using Class_DTO;

namespace Class_DAO
{
    public class DataProvider
    {
        public static SqlConnection GetConnection()
        {
            //string string_conn = "Data Source=tanphat-home-pc\\sqlexpress;Initial Catalog=QuanLySinhvien;Integrated Security=True";
            //SqlConnection conn = new SqlConnection(string_conn);
            //conn.Open();
            //return conn;
            return new SqlConnection("Data Source=THANHCHUNG-PC;Initial Catalog=QuangCaoABC;Integrated Security=True");
        
        }
        public static void OpenConnection()
        {
            SqlConnection con = GetConnection();
            try
            {
                con.Open();
            }
            catch (Exception ex)
            {
                if (con != null)
                {
                    con.Dispose();
                }
            }
        }
        public static void CloseConnection()
        {
            SqlConnection con = GetConnection();
            if (con != null)
            {
                con.Close();
            }
        }
        public static DataTable GetTable(string sql)
        {

            SqlConnection con = GetConnection();
            SqlDataAdapter ad = new SqlDataAdapter(sql, con);
            DataTable dt = new DataTable();
            ad.Fill(dt);
            return dt;
        }
        public static void ExcuteNonQuery(string sql)
        {
            OpenConnection();        
            SqlCommand cmd = new SqlCommand(sql);
            cmd.ExecuteNonQuery();
            cmd.Dispose();
            CloseConnection();
        }
        public static void proc(string s, SqlParameter[] sp)
        {
            SqlConnection con = GetConnection();
            con.Open();
            SqlCommand cmd = new SqlCommand(s, con);
            cmd.CommandType = CommandType.StoredProcedure;
            foreach (SqlParameter s1 in sp)
            {
                cmd.Parameters.Add(s1);
            }
            cmd.ExecuteNonQuery();
            CloseConnection();
        }
        public static string GetBaoCao()
        {
           
            return   "Data Source=THANHCHUNG-PC;Initial Catalog=QuangCaoABC;Integrated Security=True";

        }
    }
}
